-- sfe_presetdata_utils.lua
require "dumper"
require "mainfunctions"

local sfe_presetdata_utils = {}

--- 保存复杂数据到持久化存储
--- @param data table 要保存的数据
function sfe_presetdata_utils:Save(data)
    local serialized = DataDumper(data, nil, true)
    SavePersistentString("sfe_presetdata_list", serialized, false)
end


--- 从持久化存储中读取复杂数据
function sfe_presetdata_utils:Load(callback)
    TheSim:GetPersistentString("sfe_presetdata_list", function(success, str)
        if success and str then
            local success, data = RunInSandboxSafe(str)
            if success and data then

                -- 检查整个预设列表中的每个预设版本
                sfe_presetdata_utils:ValidatePresetList(data)

                callback(true, data)
            else
                print("反序列化失败")
                callback(false, "Deserialize failed")
            end
        else
            print("加载失败")
            callback(false, "Load failed")
        end
    end)
end

--- 获取默认预设模板
--- @return table 默认模板
function sfe_presetdata_utils:GetDefaultTemplate()
    return {
        -- order = {"默认模板1","默认模板2","默认模板3","默认模板4"},
        -- list = {
        --     ["默认模板"] = {
        --         version = 1.1,
        --         name = "默认模板",
        --         description = "这是Mod作者留下的一个默认预设",
        --         CollectRule = {
        --             IgnoreWeapon = false,
        --             IgnoreArmor = false,
        --             IgnoreAmulet = false,
        --             IgnoreUnStackable = false,
        --             IgnoreFood = false,
        --             IgnoreSomething = false,
        --         },
        --         CollectMode = {
        --             MatchMode = true,
        --         },
        --         ItemWhiteList = {},
        --         ItemBlackList = {}
        --     },
        -- },
        order = {},
        list = {},
    }
end

--- 验证单个预设的版本
--- @param preset table 单个预设对象
function sfe_presetdata_utils:ValidatePreset(preset)
    if not preset.version or preset.version == 1.0 then
        print("检测到旧版本预设：" .. (preset.name or "unnamed"))
        -- TODO: 添加版本迁移函数
        -- self:MigratePreset(preset)
    end
end

--- 验证整个预设列表的版本
function sfe_presetdata_utils:ValidatePresetList(data)
    if data and data.list then
        for k, preset in pairs(data.list) do
            self:ValidatePreset(preset)
        end
    end
end

-- 添加或更新预设
function sfe_presetdata_utils:AddPreset(data, preset)
    assert(type(preset) == "table", "预设数据必须为 table")
    assert(type(data) == "table", "数据必须为 table")

    if not data.list then data.list = {} end
    if not data.order then data.order = {} end

    local exists = data.list[preset.name] ~= nil

    -- 无论是否存在，都更新数据
    data.list[preset.name] = preset

    -- 仅在首次添加时插入到 order
    if not exists then
        table.insert(data.order, preset.name)
    end

    return data
end

-- 删除预设
function sfe_presetdata_utils:RemovePreset(data, name)
    -- 从 list 中移除预设
    if data.list[name] then
        data.list[name] = nil
    end

    -- 从 order 数组中移除所有对应的 name
    for i = #data.order, 1, -1 do
        if data.order[i] == name then
            table.remove(data.order, i)
        end
    end

    return data
end

return sfe_presetdata_utils